﻿using BodyFormat;
using Data;
using Noesis.Javascript;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JavaScript
{
    static class JavaScript
    {

        public static string BuildData = "";

        public static bool JavaScriptBuild(RequestParameteData _rpd)
        {

            using (JavascriptContext context = new JavascriptContext())
            {
                try
                {
                    context.SetParameter("Heads", new DebugHeads(_rpd));
                    context.SetParameter("Bodys", new DebugBodys(_rpd));
                    context.SetParameter("FormData", new DebugFormData(_rpd));
                    context.SetParameter("KeyVal", new BodyFormat.KeyValue());
                    context.SetParameter("$", new AssemblyLoad.AssemblyLoad());
                    BuildAllGlobalCode.BuildGlobalCode(context);
                    context.Run(_rpd.requestCode);
                    context.Run("$BpAG0xRn = Main()");
                    //Console.WriteLine(context.GetParameter("$BpAG0xRn").ToString());
                    BuildData = context.GetParameter("$BpAG0xRn").ToString();
                    Console.WriteLine(BuildData);
                    return true;
                }
                catch (Noesis.Javascript.JavascriptException e)
                {
                    //MessageBox.Show("脚本错误:" + e.Message + e.Line);
                    //Console.WriteLine(e.Message + e.Line);
                    BuildData = e.Message + e.Line;
                    return false;
                }
                catch (Exception e) {
                    BuildData = e.Message;
                    return false;
                }
            }
        }


    }
}
